package com.ruoyi.web.controller.wechat;

import cn.hutool.json.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.tag.WxUserTag;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: yan
 * @Description: 用户标签
 * @Date: create in 2021/11/4 11:00
 */
@Slf4j
@RestController
@RequestMapping("/wechat/wxUserTags")
@RequiredArgsConstructor
@Api(tags = {"标签管理"})
public class WxUserTagsController {

    private final WxMpService wxMpService;


    /**
     * 查询用户标签
     */
    @GetMapping("/list/{appId}")
    @ApiOperation(value = "查询用户标签")
    public AjaxResult list(@PathVariable String appId) {
        log.info("用户标签进来了:{}", appId);
        try {
            wxMpService.switchover(appId);
            List<WxUserTag> wxUserTags = wxMpService.getUserTagService().tagGet();
            log.info("用户标签:{}", wxUserTags);
            return AjaxResult.success("list", wxUserTags);
        } catch (WxErrorException e) {
            log.error("获取标签异常:" + e);
            return AjaxResult.error("获取标签失败");
        }

    }

    /**
     * 获取微信用户标签
     *
     * @return
     */
    @ApiOperation(value = "获取微信用户标签")
    @GetMapping("/list")
    public AjaxResult getWxUserList(@RequestParam("appid") String appId) {
        log.info("获取微信用户标签,appid={}", appId);
        try {
            this.wxMpService.switchover(appId);
            List<WxUserTag> listWxUserTag = wxMpService.getUserTagService().tagGet();
            log.info("标签:{}", listWxUserTag);
            return AjaxResult.success(listWxUserTag);
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("获取微信用户标签失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 新增微信用户标签
     *
     * @return
     */
    @ApiOperation(value = "新增微信用户标签")
    @PostMapping
    public AjaxResult save(@RequestBody JSONObject data) {
        String appId = data.getStr("appid");
        String name = data.getStr("name");
        wxMpService.switchoverTo(appId);
        try {
            return AjaxResult.success(wxMpService.getUserTagService().tagCreate(name));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("新增微信用户标签失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改微信用户标签
     *
     * @return
     */
    @ApiOperation(value = "修改微信用户标签")
    @PutMapping
    public AjaxResult updateById(@RequestBody JSONObject data) {
        String appId = data.getStr("appid");
        Long id = data.getLong("id");
        String name = data.getStr("name");
        try {
            wxMpService.switchover(appId);
            return AjaxResult.success(wxMpService.getUserTagService().tagUpdate(id, name));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("修改微信用户标签失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 删除微信用户标签
     *
     * @param id
     * @param appId
     * @return
     */
    @ApiOperation(value = "删除微信用户标签")
    @DeleteMapping
    public AjaxResult removeById(@Param("id") Long id, @Param("appid") String appId) {
        log.info("删除用户标签的参数，{}，{}",id,appId);
        wxMpService.switchover(appId);
        try {
            return AjaxResult.success(wxMpService.getUserTagService().tagDelete(id));
        } catch (WxErrorException e) {
            e.printStackTrace();
            log.error("删除微信用户标签失败", e);
            return AjaxResult.error(e.getMessage());
        }
    }

}
